package org.example.controller;

import org.example.entity.Order;
import org.example.mapper.OrderMapper;
import org.example.mapper.UserMapper;
import org.example.entity.User;
import org.mockito.internal.matchers.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.ParameterizedType;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
public class TestController {

    @Autowired
    private ApplicationContext applicationContext;



    @RequestMapping("/test")
    public void test()
    {
        UserMapper userMapper = (UserMapper) applicationContext.getBean("UserMapper");
        List<User> list = userMapper.getList();
        for (User user : list) {
            System.out.println(user);
        }
    }

    @RequestMapping("/cc")
    public User getOne(Integer id)
    {
        System.out.println(id);
        User user = new User();
        user.setId(id);
        UserMapper userMapper = (UserMapper) applicationContext.getBean("UserMapper");
        User one = userMapper.getOne(user);
        return one;
    }

    @RequestMapping("/order/list")
    public List<Order> list()
    {
        OrderMapper orderMapper = (OrderMapper) applicationContext.getBean("OrderMapper");
        List<Order> list = orderMapper.getList();
        return list;
    }

    @RequestMapping("/order/update")
    public Integer update(Integer id)
    {
        OrderMapper orderMapper = (OrderMapper) applicationContext.getBean("OrderMapper");
        Order order = new Order();
        order.setId(id);
        order.setOrders("cc");
        //order.setDate(new Date());
        return orderMapper.update(order);
    }

    @RequestMapping("/order/delete")
    public Integer delete(Integer id) {
        OrderMapper orderMapper = (OrderMapper) applicationContext.getBean("OrderMapper");
        return orderMapper.delete(id);
    }
    @RequestMapping("/order/insert")
     public Integer insert()
    {
        OrderMapper orderMapper = (OrderMapper) applicationContext.getBean("OrderMapper");
        Order order = new Order();
        order.setId(66);
        order.setDate(LocalDateTime.now());
        order.setOrders("pp");
        return orderMapper.insert(order);
    }
}
